package com.gss.plugin.message.websocket;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: DYang
 * @since: 2023/8/24
 * @description:
 */
@RestController
@RequestMapping("/websocket")
public class WebsocketController {

    /**
     * 推送数据
     * @param info
     * @return
     */
    @PostMapping("/pushMsg")
    public boolean pushMsg(@RequestBody MsgInfo info){
        if (info == null || info.getUserIds() == null || info.getData() == null) {
            return false;
        }
        // 判断是广播还是用指定用户推送
        if (info.isBroadcast()) {
            // 广播推送
            WebSocketServer.sendAllMessage(JSONObject.toJSONString(info.getData()));
        }else {
            // 指定用户推送
            String[] arr = info.getUserIds().split(",");
            for (String userId : arr) {
                WebSocketServer.sendMessageByUser(userId,JSONObject.toJSONString(info.getData()));
            }
        }
        return true;
    }

}
